/**
 * 
 */
package user.web.zconfigurer;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.beanvalidation.BeanValidationPostProcessor;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;

import user.web.filter.UserAuthFilter;

/**
 * @author Eric
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = { WebMvcConfigurer.class })
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
	
//	@Override
//	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
//		argumentResolvers.add(new AuthenticationPrincipalArgumentResolver());
//	}
	
	@Autowired
	private UserAuthFilter userAuthFilter;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		
		registry.addInterceptor(userAuthFilter).addPathPatterns("/**")
			.excludePathPatterns("/user/login")
			.excludePathPatterns("/user/phoneValid")
			.excludePathPatterns("/user/regist");
		
		super.addInterceptors(registry);
	}
	
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4 = new FastJsonHttpMessageConverter4();
		fastJsonHttpMessageConverter4.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON_UTF8, MediaType.TEXT_HTML));
		fastJsonHttpMessageConverter4.setDefaultCharset(StandardCharsets.UTF_8);
		
		converters.add(fastJsonHttpMessageConverter4);
	}
	
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**/*")
			.allowedMethods("GET", "POST")
			.allowedOrigins("*")
			.allowedHeaders("*")
			.maxAge(Integer.MAX_VALUE);
	}
	
	@Bean
	public BeanValidationPostProcessor beanValidationPostProcessor() {
		return new BeanValidationPostProcessor();
	}
}

